package cn.itcast.web.controller.cargo;

import cn.itcast.common.utils.BeanMapUtils;
import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.ExportProduct;
import cn.itcast.domain.cargo.ExportProductExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cargo/export")
public class PdfController extends BaseController {

    @Reference
    private ExportService exportService;

    @Reference
    private ExportProductService exportProductService;

    /**
     * 报运单PDF文件生成
     *      参数：报运单id
     */
    @RequestMapping("/exportPdf")
    public void exportPdf(String id)throws Exception{
        //1.根据id查询报运单
        Export export = exportService.findById(id);
        //2.根据报运单id查询所有的商品
        ExportProductExample example = new ExportProductExample();
        ExportProductExample.Criteria criteria = example.createCriteria();
        criteria.andExportIdEqualTo(id);
        List<ExportProduct> list = exportProductService.findAll(example);
        //3.获取模版文件的路径
        String path = session.getServletContext().getRealPath("/")+"/jasper/export.jasper";
        //4.根据模版生成jasperPrint对象（完成数据填充）
        //4.1 报运单对象
        Map params = BeanMapUtils.beanToMap(export);
        //4.2 商品list集合
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);
        //进行数据填充
        JasperPrint jasperPrint = JasperFillManager.fillReport(path,params,dataSource); //1.文件路径 2、map类型参数  3.数据源类型参数
        //5.生成PDF文件 预览
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }




    /**
     * 特殊模板 chart图表
     */
  /*  @RequestMapping("/exportPdf")
    public void exportPdf() throws Exception{
        //1.获取模版文件的路径
        String path = session.getServletContext().getRealPath("/")+ "/jasper/test05.jasper";
        //2.根据模版生成jasperPrint对象（完成数据填充）

            //传递一个map集合
        List<Map> list = new ArrayList<>();

        for (int i = 0; i < 5; i++) {
            Map map = new HashMap();
            map.put("name","名称"+i);
            map.put("count",10*(i+1));
            list.add(map);
        }
        //list集合数据填充
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);

        JasperPrint jasperPrint = JasperFillManager.fillReport(path,new HashMap<>(),dataSource); //1.文件路径 2.map类型参数  3.数据源类型参数
        //3.生成PDF文件预览
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());

    }*/
/*
    @RequestMapping("/exportPdf")
    public void exportPdf() throws Exception{
        //1.获取模版文件的路径
        String path = session.getServletContext().getRealPath("/")+ "/jasper/test05.jasper";
        //2.根据模版生成jasperPrint对象（完成数据填充）
        //数据准备：展示用户数据列表
        List<Map> list = new ArrayList<>();

        for (int i=0 ;i<5 ;i++) {
          Map map = new HashMap();
          map.put("name","企业"+i);
          map.put("count",10 *(i+1));
          list.add(map);
        }

        //list集合数据填充
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);

        JasperPrint jasperPrint = JasperFillManager.fillReport(path,new HashMap<>(),dataSource); //1.文件路径 2.map类型参数  3.数据源类型参数
        //3.生成PDF文件预览
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }
*/



    /**
     * map类型数填充
     */
 /*   @RequestMapping("/exportPdf2")
    public void exportPdf() throws Exception{
        //1.获取模版文件的路径
        String path = session.getServletContext().getRealPath("/")+ "/jasper/test02.jasper";
        //2.根据模版生成jasperPrint对象（完成数据填充）
        Map param = new HashMap<String,Object>();
        param.put("username","张三");
        param.put("age",12);
        param.put("address","北京");
        param.put("phone","111");

        JasperPrint jasperPrint = JasperFillManager.fillReport(path,param,new JREmptyDataSource()); //1.文件路径 2.map类型参数  3.数据源类型参数
        //3.生成PDF文件预览
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }*/






 /*   *//**
     * list集合类型数据填充
     *          JRDataSource数据参数
     *//*
    @RequestMapping("/exportPdf")
    public void exportPdf() throws Exception{
        //1.获取模版文件的路径
        String path = session.getServletContext().getRealPath("/")+ "/jasper/test04.jasper";
        //2.根据模版生成jasperPrint对象（完成数据填充）
        //数据准备：展示用户数据列表
        List<User> list = new ArrayList<>();

        for (int i=0 ;i<5 ;i++) {
            User user = new User();
            user.setUserName("测试"+ i);
            user.setCompanyName("传智播客");
            user.setEmail(i+"@itcast.cn");
            list.add(user);
        }

        for (int i=0 ;i< 3 ;i++) {
            User user = new User();
            user.setUserName("员工"+i);
            user.setCompanyName("it黑马");
            user.setEmail(i+"@itcast.cn");
            list.add(user);
        }
        //list集合数据填充
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);

        JasperPrint jasperPrint = JasperFillManager.fillReport(path,new HashMap<>(),dataSource); //1.文件路径 2.map类型参数  3.数据源类型参数
        //3.生成PDF文件预览
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }*/



    /**
     * map类型数填充
     */
 /*   @RequestMapping("/exportPdf2")
    public void exportPdf() throws Exception{
        //1.获取模版文件的路径
        String path = session.getServletContext().getRealPath("/")+ "/jasper/test02.jasper";
        //2.根据模版生成jasperPrint对象（完成数据填充）
        Map param = new HashMap<String,Object>();
        param.put("username","张三");
        param.put("age",12);
        param.put("address","北京");
        param.put("phone","111");

        JasperPrint jasperPrint = JasperFillManager.fillReport(path,param,new JREmptyDataSource()); //1.文件路径 2.map类型参数  3.数据源类型参数
        //3.生成PDF文件预览
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }*/

    /**
     * 返回值：下载或者以PDF方式预览
     */

 /*   @RequestMapping("/exportPdf1")
    public void exportPdf() throws Exception{
        //1.获取模版文件的路径
        String path = session.getServletContext().getRealPath("/")+ "/jasper/test01.jasper";
        //2.根据模版生成jasperPrint对象（完成数据填充）
        JasperPrint jasperPrint = JasperFillManager.fillReport(path,new HashMap<String, Object>(),new JREmptyDataSource()); //1.文件路径 2.map类型参数  3.数据源类型参数
        //3.生成PDF文件预览
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }*/

}
